package org.lc.stk.service.paper.impl;

import java.util.List;

import org.lc.stk.dao.paper.PaperTemplateRepository;
import org.lc.stk.model.paper.attr.PaperTemplate;
import org.lc.stk.service.exception.BusinessException;
import org.lc.stk.service.paper.PaperTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PaperTemplateServiceImpl implements PaperTemplateService {

    @Autowired
    private PaperTemplateRepository paperTemplateRepository;

    @Override
    @Transactional
    public PaperTemplate create(PaperTemplate template) {
        // TODO: 验证试卷模板数据
        return paperTemplateRepository.save(template);
    }

    @Override
    @Transactional
    public PaperTemplate update(PaperTemplate template) {
        if (!paperTemplateRepository.existsById(template.getId())) {
            throw new BusinessException("试卷模板不存在");
        }
        // TODO: 验证试卷模板数据
        return paperTemplateRepository.save(template);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        if (!paperTemplateRepository.existsById(id)) {
            throw new BusinessException("试卷模板不存在");
        }
        // TODO: 检查试卷模板是否被使用
        paperTemplateRepository.deleteById(id);
    }

    @Override
    public PaperTemplate findById(Integer id) {
        return paperTemplateRepository.findById(id)
            .orElseThrow(() -> new BusinessException("试卷模板不存在"));
    }

    @Override
    public List<PaperTemplate> findByBankId(Integer bankId) {
        return paperTemplateRepository.findByBanksId(bankId);
    }

    @Override
    public boolean existsById(Integer id) {
        return paperTemplateRepository.existsById(id);
    }
}
